package hos.filex.delegate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultCaller;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.List;

import hos.file.FileCollection;
import hos.file.media.FileType;
import hos.file.media.MediaUri;
import hos.intent.IntentCollection;
import hos.intent.IntentResult;

/**
 * <p>Title: FileResultContracts </p>
 * <p>Description: 文件选择 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-14 10:03
 */
public class FileResultContracts {

    /**
     * 创建一个返回值得跳转
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<Intent> start(ActivityResultCaller caller,
                                                       ActivityResultCallback<ActivityResult> callback) {
        return caller.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), callback);
    }
    //<editor-fold desc="Camera">

    /**
     * 拍照
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<FileType.Type> choosePickMedia(ActivityResultCaller caller, ActivityResultCallback<Uri> callback) {
        return caller.registerForActivityResult(new PickMedia(), callback);
    }

    /**
     * 拍照
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> createCameraImage(ActivityResultCaller caller, ActivityResultCallback<Uri> callback) {
        return caller.registerForActivityResult(new CameraImage(), callback);
    }

    /**
     * 录音
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> createCameraAudio(ActivityResultCaller caller, ActivityResultCallback<Uri> callback) {
        return caller.registerForActivityResult(new CameraAudio(), callback);
    }

    /**
     * 摄像
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> createCameraVideo(ActivityResultCaller caller, ActivityResultCallback<Uri> callback) {
        return caller.registerForActivityResult(new CameraVideo(), callback);
    }
    //</editor-fold>

    //<editor-fold desc="OpenFile">

    /**
     * 打开定位设置界面
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<Void> openLocationSetting(ActivityResultCaller caller, ActivityResultCallback<Boolean> callback) {
        return caller.registerForActivityResult(new OpenLocationSetting(), callback);
    }

    /**
     * 打开文件
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> openFile(ActivityResultCaller caller, ActivityResultCallback<Boolean> callback) {
        return caller.registerForActivityResult(new OpenFile(), callback);
    }

    /**
     * 打开WPS
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> openWps(ActivityResultCaller caller, ActivityResultCallback<Boolean> callback) {
        return caller.registerForActivityResult(new OpenWps(), callback);
    }

    /**
     * 打开Url
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> openUrl(ActivityResultCaller caller, ActivityResultCallback<Boolean> callback) {
        return caller.registerForActivityResult(new OpenUrl(), callback);
    }

    /**
     * 打开 应用市场
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<String> openMarket(ActivityResultCaller caller, ActivityResultCallback<Boolean> callback) {
        return caller.registerForActivityResult(new OpenMarket(), callback);
    }
    //</editor-fold>

    //<editor-fold desc="Content">

    /**
     * 打开 文件管理器 选择文件 根据文件类型
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    public static ActivityResultLauncher<FileType.Type> getContent(ActivityResultCaller caller, ActivityResultCallback<Uri> callback) {
        return caller.registerForActivityResult(new GetContent(), callback);
    }

    /**
     * 打开 文件管理器 选择文件 根据文件类型
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static ActivityResultLauncher<FileType.Type> getContentList(ActivityResultCaller caller, ActivityResultCallback<List<Uri>> callback) {
        return caller.registerForActivityResult(new GetContentList(), callback);
    }

    //</editor-fold>

    //<editor-fold desc="Document">

    /**
     * 打开 文件管理器 选择文件 根据文件类型
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static ActivityResultLauncher<FileType.Type[]> getDocument(ActivityResultCaller caller, ActivityResultCallback<Uri> callback) {
        return caller.registerForActivityResult(new GetDocument(), callback);
    }

    /**
     * 打开 文件管理器 选择文件 根据文件类型
     *
     * @param caller   跳转接口
     * @param callback 回调
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static ActivityResultLauncher<FileType.Type[]> getDocumentList(ActivityResultCaller caller, ActivityResultCallback<List<Uri>> callback) {
        return caller.registerForActivityResult(new GetDocumentList(), callback);
    }


    //</editor-fold>

    //<editor-fold desc="CameraContract">
    public static class CameraImage extends ActivityResultContract<String, Uri> {
        @Nullable
        private Uri uri;

        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (TextUtils.isEmpty(input)) {
                uri = MediaUri.createImageUri(context, MediaUri.createImageName());
            } else {
                uri = MediaUri.createImageUri(context, input);
            }
            return IntentCollection.getImageMedia(uri);
        }

        @Override
        public Uri parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getMediaUri(resultCode, intent, uri);
        }
    }

    public static class CameraVideo extends ActivityResultContract<String, Uri> {
        @Nullable
        private Uri uri;

        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (TextUtils.isEmpty(input)) {
                uri = MediaUri.createVideoUri(context, MediaUri.createImageName());
            } else {
                uri = MediaUri.createVideoUri(context, input);
            }
            return IntentCollection.getVideoMedia(uri);
        }

        @Override
        public Uri parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getMediaUri(resultCode, intent, uri);
        }
    }

    public static class CameraAudio extends ActivityResultContract<String, Uri> {
        @Nullable
        private Uri uri;

        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (TextUtils.isEmpty(input)) {
                uri = MediaUri.createAudioUri(context, MediaUri.createImageName());
            } else {
                uri = MediaUri.createAudioUri(context, input);
            }
            return IntentCollection.getAudioMedia(uri);
        }

        @Override
        public Uri parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getMediaUri(resultCode, intent, uri);
        }
    }

    public static class PickMedia extends ActivityResultContract<FileType.Type, Uri> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, FileType.Type input) {
            return FileCollection.getPickMedia(input);
        }

        @Override
        public Uri parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getFileUri(resultCode, intent);
        }
    }
    //</editor-fold>


    //<editor-fold desc="OpenContract">
    public static class OpenLocationSetting extends ActivityResultContract<Void, Boolean> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, Void input) {
            return IntentCollection.getLocationSetting();
        }

        @Override
        public Boolean parseResult(int resultCode, @Nullable Intent intent) {
            return resultCode == Activity.RESULT_OK;
        }
    }

    public static class OpenFile extends ActivityResultContract<String, Boolean> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (input == null || input.length() == 0) {
                throw new NullPointerException(" filePath is null");
            }
            return FileCollection.getFile(context, input);
        }

        @Override
        public Boolean parseResult(int resultCode, @Nullable Intent intent) {
            return resultCode == Activity.RESULT_OK;
        }
    }

    public static class OpenWps extends ActivityResultContract<String, Boolean> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (input == null || input.length() == 0) {
                throw new NullPointerException(" filePath is null");
            }
            return FileCollection.getWps(context, input);
        }

        @Override
        public Boolean parseResult(int resultCode, @Nullable Intent intent) {
            return resultCode == Activity.RESULT_OK;
        }
    }

    public static class OpenUrl extends ActivityResultContract<String, Boolean> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (input == null || input.length() == 0) {
                throw new NullPointerException(" url is null");
            }
            return IntentCollection.getUrl(input);
        }

        @Override
        public Boolean parseResult(int resultCode, @Nullable Intent intent) {
            return resultCode == Activity.RESULT_OK;
        }
    }

    public static class OpenMarket extends ActivityResultContract<String, Boolean> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String input) {
            if (input == null || input.length() == 0) {
                throw new NullPointerException(" packageName is null");
            }
            return IntentCollection.getMarket(input);
        }

        @Override
        public Boolean parseResult(int resultCode, @Nullable Intent intent) {
            return resultCode == Activity.RESULT_OK;
        }
    }
    //</editor-fold>

    //<editor-fold desc="ContentContract">
    public static class GetContent extends ActivityResultContract<FileType.Type, Uri> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, @NonNull FileType.Type input) {
            return FileCollection.getContentIntent(input);
        }

        @Nullable
        @Override
        public final Uri parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getFileUri(resultCode, intent);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static class GetContentList extends ActivityResultContract<FileType.Type, List<Uri>> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, @NonNull FileType.Type input) {
            return FileCollection.getContentListIntent(input);
        }

        @Nullable
        @Override
        public final List<Uri> parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getFileListUri(resultCode, intent);
        }
    }

    //</editor-fold>

    //<editor-fold desc="DocumentContract">

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static class GetDocument extends ActivityResultContract<FileType.Type[], Uri> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, @NonNull FileType.Type[] input) {
            return FileCollection.getDocumentIntent(input);
        }

        @Nullable
        @Override
        public final Uri parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getFileUri(resultCode, intent);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static class GetDocumentList extends ActivityResultContract<FileType.Type[], List<Uri>> {
        @CallSuper
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, @NonNull FileType.Type[] input) {
            return FileCollection.getDocumentListIntent(input);
        }

        @Nullable
        @Override
        public final List<Uri> parseResult(int resultCode, @Nullable Intent intent) {
            return IntentResult.getFileListUri(resultCode, intent);
        }
    }
    //</editor-fold>


}
